//
//  statusRetweetedView.swift
//  sinazd
//
//  Created by apple on 16/3/13.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

import SnapKit
    
class StatusRetweetedView: UIView {
    
    var bottomConstraint:Constraint?
    
        //数据模型
        var retweetedStatus: Status? {
            didSet {
                //绑定数据
                retweetedLabel.text = retweetedStatus?.text
                
                bottomConstraint?.uninstall()
                
                if let urls = retweetedStatus?.imgUrls where urls.count != 0 {
                    pictureView.imgUrls = urls
                    
                    pictureView.hidden = false
                    
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    })
                    
                }else{
                    pictureView.imgUrls = nil
                    
                    pictureView.hidden = true
                    
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                    })
                }
                
            }
        }
        
        
        //重写构造方法
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }
        
        //默认实现报错
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        
        //MARK: 设置UI界面
        private func setupUI() {
            backgroundColor = UIColor.redColor()
            
            addSubview(retweetedLabel)
            
            addSubview(pictureView)
            
            
            //设置约束
            retweetedLabel.snp_makeConstraints { (make) -> Void in
                make.left.top.equalTo(self).offset(StatusCellMargin)
                make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            }
            
            pictureView.snp_makeConstraints { (make) -> Void in
                make.left.equalTo(retweetedLabel.snp_left)
                make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
                make.size.equalTo(CGSize(width: 100, height: 100))
            }
            
            //给转发微博的底部添加约束
            self.snp_makeConstraints { (make) -> Void in
              self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            }
        }
        
        
        //MARK: 懒加载子视图
        private lazy var retweetedLabel: UILabel = UIFactory.labelWith("听说下雨天,音乐和辣条更陪喔", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
        
        //配图视图
        private lazy var pictureView: statusPictureView = statusPictureView()
}



